<?php
/*
 * This class is used for as Model for Blog table 
 */
class Model_Category extends Model_Base {
    protected $_name = "tbl_category";
    protected $_primaryKey = "cat_id";
     
    public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
	
    
    public function fetchEntryById($id) {
		$select = $this->select()
                ->from(array("category" => "tbl_category"))
                ->where("cat_id = ?", $id)
                ->where("cat_status!= 2");
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    

    /**
     *  This function is used to get blog
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchCategory($where = "", $limit = "", $offset = "0", $order = "cat_id", $sort = "ASC") {
        $select = $this->select()
                ->from(array("category" => "tbl_category"));
               // ->where("cat_status != 2");
        if ($where != "") {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getCategoryCount($where) {
        $select = $this->select()
                ->from(array("category_" => "tbl_category"), Array("cnt" => "count(*)"));
                //->where("cat_status != 2");
        if ($where != "") {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
    /**
     *  This function is used to delete record
     * 
     */
    public function deleteCategory($category_Array) {
        if (is_array($category_Array)) {
            foreach ($category_Array as $_category) {
                $categoryData = Array();
                $categoryData = $this->fetchEntryById($_category);
                if (count($categoryData) > 0) {
                    $data = array("cat_status" => "2");
                    $this->update($data, "cat_id =" . (int) $_category);
                }
            }
        } else {
            $cat_id = $category_Array;
            $categoryData = $this->fetchEntryById($cat_id);
            if (count($categoryData) > 0) {
                $data = array("cat_status" => "2");
                $this->update($data, "cat_id =" . $cat_id);
            }
        }
    }

    public function fetchBlogWITHComment($where = "", $limit = "", $offset = "0", $order = "cat_id", $sort = "ASC") {
        $select = $this->select()
                ->from(array("category" => "tbl_category"))
                ->where("cat_status =1");
        if ($where != "") {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        $commentModel = new Model_Comment();
        $finalArray = array();
        if (!empty($data)) {
            $i = 0;
            foreach($data->toArray() as $key =>$value){
                $commnentData = $commentModel->fetchComment("cmt_cat_id = ".$value['cat_id'] );
                $finalArray[$i]['cat_id'] = $value['cat_id'];
                $finalArray[$i]['cat_name'] = $value['cat_name'];
                $finalArray[$i]['cat_createddate'] = $value['cat_createddate'];
                $finalArray[$i]['cat_updateddate'] = $value['cat_updateddate'];
                $finalArray[$i]['cat_status'] = $value['cat_status'];
                $finalArray[$i]['comment_count'] = count($commnentData);
            $i++;
                
            }
            return $finalArray;
        } else {
            return array();
        }
    }

     public function NEXTcategoryid($id) {
        $select = $this->select()
                ->from(array("category" => "tbl_category"),array("next_id" => 'min(cat_id)'))
                ->where("cat_id > ?", $id)
                ->where("cat_status!= 2");
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
     public function PREVIOUScategoryid($id) {
        $select = $this->select()
                ->from(array("category" => "tbl_category"),array("prev_id" => 'max(cat_id)'))
                ->where("cat_id < ?", $id)
                ->where("cat_status!= 2");
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
}?>
